﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using Microsoft.Samples.Kinect.WpfViewers;
using Coding4Fun.Kinect.Wpf;
using System.Windows.Ink;


namespace FGP_Kpaint
{
    class Controler
    {
        public struct brushes
        {
            public Line line2;
            public int size;

        }

        int index;
        public brushes brush, red, green, blue, yellow, brown, black, white, up, down, self;
        public brushes[] array = new brushes[10];
        Boolean flag_firstPoint, flag_checkGesture, flag_addGesture, flag_voiceCommand, flag_captureImage,flag_addMenu,
            flag_gallery, flag_galleryOn;
        KinectSensor _sensor;
        Point p;
        double pro;
        string gestType;

        
        //objects
        CheckGesture gesture;
        VoiceRecognition voice;
        Command command;
        SwipeGesture swipe;
        
        public double[,] points;
       public ImageBrush checkGestureOn,checkGestureOff,addGesture,addGestureSaving,captureImageOn,captureImageOff,voiceOn,voiceOff,
            galleryOn, galleryOff, swipeIcon, swipeIconR, swipeIconRL, swipeIconL;
        MainWindow main;
       // ImageBrush [] gestureArray;
        public Controler(MainWindow main, KinectSensor sensor)
        {
            pro = 0;
            this.main = main;
            this._sensor = sensor;
            gestType = "";
            voice = new VoiceRecognition(_sensor, this.main, this);
        
            swipe = new SwipeGesture(this.main,this);
            
            brush = new brushes();
            brush.line2 = new Line();
            brush.line2.Stroke = Brushes.Black;
            brush.size = 1;

            //red color
            red = new brushes();
            red.line2 = new Line();
            red.line2.Stroke = Brushes.Red;
            red.size = brush.size;

            //Black color
            black = new brushes();
            black.line2 = new Line();
            black.line2.Stroke = Brushes.Black;
            black.size = brush.size;

            //Brown color
            brown = new brushes();
            brown.line2 = new Line();
            brown.line2.Stroke = Brushes.Brown;
            brown.size = brush.size;

            //White color
            white = new brushes();
            white.line2 = new Line();
            white.line2.Stroke = Brushes.White;
            white.size = brush.size;

            //green color
            green = new brushes();
            green.line2 = new Line();
            green.line2.Stroke = Brushes.Green;
            green.size = brush.size;

            //blue color
            blue = new brushes();
            blue.line2 = new Line();
            blue.line2.Stroke = Brushes.Blue;
            blue.size = brush.size;

            //yellow
            yellow = new brushes();
            yellow.line2 = new Line();
            yellow.line2.Stroke = Brushes.Yellow;
            yellow.size = brush.size;

            //up
            up = new brushes();
            up.line2 = new Line();
            up.line2.Stroke = brush.line2.Stroke;
            up.size = brush.size + 1;

            //down
            down = new brushes();
            down.line2 = new Line();
            down.line2.Stroke = brush.line2.Stroke;
            down.size = brush.size - 1;

            //self
            self = new brushes();
            self.line2 = new Line();
            self.line2.Stroke = brush.line2.Stroke;
            self.size = brush.size;

            //array to hold struct values
            array[0] = red;
            array[1] = blue;
            array[2] = green;
            array[3] = yellow;
            array[4] = brown;
            array[5] = white;
            array[6] = black;
            array[7] = up;
            array[8] = down;
            array[9] = self;

            //end struct--------------------------------------

            //index to be used to retrieve right struct from array
            index = 0;

            flag_firstPoint = true;
            gesture = new CheckGesture();
            gesture.LoadGesture(main);
            points = new double[4, 2];
            for (int i = 0; i < 4; i++)
            {
                points[i, 0] = 0;
                points[i, 1] = 0;
            }
            flag_checkGesture = false;
            flag_addGesture = false;
            flag_voiceCommand = true;
            flag_captureImage = false;
            flag_gallery = false;
            flag_addMenu = false;

            checkGestureOn = new ImageBrush();
            checkGestureOn.ImageSource =
                new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\gesture2.jpg"));

            checkGestureOff = new ImageBrush();
            checkGestureOff.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\gesture1.jpg"));

            addGesture = new ImageBrush();
            addGesture.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\Add1.jpg"));

            addGestureSaving = new ImageBrush();
            addGestureSaving.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\Add2.jpg"));

            captureImageOn = new ImageBrush();
            captureImageOn.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\Cam2.jpg"));
            
            captureImageOff = new ImageBrush();
            captureImageOff.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\Cam1.jpg"));

            voiceOn = new ImageBrush();
            voiceOn.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\voice1.jpg"));

            voiceOff = new ImageBrush();
            voiceOff.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\voice2.jpg"));

            galleryOff = new ImageBrush();
            galleryOff.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\galleryOff.jpg"));

            galleryOn = new ImageBrush();
            galleryOn.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\galleryOn.jpg"));

            swipeIcon = new ImageBrush();
            swipeIcon.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\swipeGesture1.jpg"));

            swipeIconR = new ImageBrush();
            swipeIconR.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\swipeGesture2.jpg"));

            swipeIconRL = new ImageBrush();
            swipeIconRL.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\swipeGesture3.jpg"));

            swipeIconL = new ImageBrush();
            swipeIconL.ImageSource =
               new BitmapImage(new Uri("C:\\Users\\chaos\\Documents\\Visual Studio 2010\\Projects\\FGP_Kpaint\\FGP_Kpaint\\NewFolder1\\swipeGesture4.jpg"));

           
            command = new Command(main);
          
        }

 

        public string checkGesture()
        {
            string res = "Wait";

            
            if (pro >= 100)
            {

                res = gesture.checking(points);
                pro = 0;
                main.progressBar1.Value = 0;
                flag_checkGesture = false;
                flag_addMenu = false;
                main.ellipse8.Fill = checkGestureOff;
            }

            main.progressBar1.Value = pro;
            pro = pro + 1;
            return res;
        }

        public string addGestures()
        {
            string res = "Wait";

         
            if (pro >= 100)
            {

                gesture.AddGesture(gestType, points);
                pro = 0;
                main.progressBar1.Value = 0;
                flag_addGesture = false;
                flag_addMenu = false;
                main.ellipse13.Fill = addGesture;
                gesture.LoadGesture(main);
                main.Combo.Visibility = System.Windows.Visibility.Collapsed;
                 res=gestType+"  is ADD successfully";
               
            }
            main.progressBar1.Value = pro;
            pro = pro + 1;

            return res;
        }

        //switching voice commands on and off
        public string voiceCommand()
        {
            string res = "Wait";
            //main.progressBar1.Value = pro;
            //pro = pro + .5;
            // if (pro == 100)
            //  {   

            if (flag_voiceCommand)
            {
                //start voice command
                voice.init_voicRecognition();
                voice.start_voiceRecognition();
                pro = 0;
                main.progressBar1.Value = 0;
                flag_voiceCommand = false;
                main.ellipse7.Fill = voiceOn;
            }
            else
            {

                //stops voice command

                flag_voiceCommand = true;
                voice.stop_voiceRecognition();
                main.ellipse7.Fill = voiceOff;
                res = "off";

            }
            return res;
        }

        //switching photo gallery on and off
        public string gallery()
        {
            string res = "Wait";

            if (flag_gallery)
            {
                //stops photo gallery
                flag_gallery = false;
                main.photogallery.Fill = galleryOff;
                swipe.stopImage();
                res = "off";
            }
            else
            {
                pro = 0;
                main.progressBar1.Value = 0;
                flag_gallery = true;
                main.photogallery.Fill = galleryOn;
                main.progressBar2.Visibility = Visibility.Visible;
                main.textBlock4.Visibility = Visibility.Visible;
            }
            return res;
        }

       public void setFlag_gallery(bool flag) {
            this.flag_gallery = flag;
            main.progressBar2.Visibility = Visibility.Hidden;
            main.textBlock4.Visibility = Visibility.Hidden;
            main.photogallery.Fill = galleryOff;

        }

        public Boolean check(ColorImagePoint color_right, ColorImagePoint Elbow_right, DepthImagePoint right, DepthImagePoint Elbow)
        {
           // points[0, 0] = color_right.X;
           // points[0, 1] = color_right.Y;
           // points[0, 2] = right.Depth;


            if (flag_checkGesture)
            {
                string c = "";

                if (flag_addMenu)
                {
                    if (Convert.ToInt32(pro) == Convert.ToInt32((pro / 25)) * 25)
                    {

                        if (pro != 100)
                        {
                            points[Convert.ToInt32(pro) / 25, 0] = color_right.X - Elbow_right.X;
                            points[Convert.ToInt32(pro) / 25, 1] = color_right.Y - Elbow_right.Y;
                        }
                    }

                    main.ellipse8.Fill = checkGestureOn;
                    c = checkGesture();

                    if (c.Contains("save"))
                    {
                        command.save();
                        main.label1.Content = "save successfully";
                    }

                    else if (c.Contains("capture"))
                    {
                        main.label1.Content = "save Capture";
                        flag_captureImage = true;

                    }
                    else if (c.Contains("close"))
                    {
                        command.close();
                    }
                    main.label1.Content = c;
                }


                else if (pro >= 100)
                {
                    flag_addMenu = true;
                    main.progressBar1.Value = 0;
                    pro = 0;

                }
                else { pro = pro + 3; main.progressBar1.Value = pro; }
                return true;

            }

            else if (flag_addGesture)
            {

                if (flag_addMenu)
                {
                    if (Convert.ToInt32(pro) == Convert.ToInt32((pro / 25)) * 25)
                    {

                        if (pro != 100)
                        {
                            points[Convert.ToInt32(pro) / 25, 0] = color_right.X - Elbow_right.X;
                            points[Convert.ToInt32(pro) / 25, 1] = color_right.Y - Elbow_right.Y;
                        }
                    }
                    main.label1.Content = addGestures();
                }


                else if (pro >= 100)
                {
                    flag_addMenu = true;
                    main.progressBar1.Value = 0;
                    pro = 0;

                }
                else { pro = pro + 3; main.progressBar1.Value = pro; }
                return true;

            
            }

            else if (flag_captureImage)
            {
                if (!command.Capture()) { main.ellipse14.Fill = captureImageOff; }
                else { main.ellipse14.Fill = captureImageOn; flag_captureImage = false; }

               // main.label2.Content = flag5;

                return true;
            }else if(flag_gallery){

                if (right.Y < Elbow.Y)
                {
                    swipe.runWorker();
                    swipe.setLRhand(1);
                    swipe.setRight_Hand(right.X);
                    swipe.setRight_Shoulder(main.rightShoulder.X);
                    swipe.setHead(main.head.X);
                    swipe.setLRhand(1);
                  //  main.textBlock4.Text = swipe.getRes();
                }

                if (main.left.Y < main.leftElbow.Y)
                {
                    swipe.runWorker();
                    swipe.setLRhand(2);
                    swipe.setLeft(main.left.X);
                    swipe.setleft_Shoulder(main.leftShoulder.X);
                    swipe.setHead(main.head.X);
                    swipe.setLRhand(2);
                  //  main.textBlock4.Text = swipe.getRes();
                }
            
            
            }


            else if (color_right.X > 0 && color_right.Y < 9)
            {
                pro = pro + 2;
                main.progressBar1.Value = pro;
                flag_firstPoint = true;


                if (pro == 100)
                {

                    main.progressBar1.Value = 0;
                    pro = 0;
                    index = color_right.X / 88;
                    // main.textBlock3.Text = index.ToString();
                   // main.label1.Content = color_right.X / 88;

                    if (index < 9)
                    {

                      // main.label1.Content = index;
                        brush.line2.Stroke = array[index].line2.Stroke;
                        brush.size = array[index].size;

                        return true;
                    }
                    else
                    {
                       // main.label2.Content = index;

                        if (index == 9)
                        {
                            //check gesture
                            main.progressBar1.Value = 0;
                            pro = 0;
                             flag_checkGesture = true;
                        }
                        else if (index == 10)
                        {
                            //add gesture
                            main.progressBar1.Value = 0;
                            pro = 0;
                            main.Combo.Visibility = System.Windows.Visibility.Visible;
                            main.ellipse13.Fill = addGestureSaving;
                          //  flag_addGesture = true;

                        }
                        else if (index == 11)
                        {
                            //enable voice commands
                            main.progressBar1.Value = 0;
                            pro = 0;
                            voiceCommand();

                        }
                        else if (index == 12)
                        {
                            //camera capture
                            main.progressBar1.Value = 0;
                            pro = 0;
                            flag_captureImage = true;
                        }
                        else if (index == 13)
                        {
                            //clear
                            main.progressBar1.Value = 0;
                            pro = 0;
                            main.canvas1.Children.Clear();
                            main.canvas1.Children.Add(main.ink);
                            main.canvas1.Children.Add(main.image3);
                        }
                        else if (index == 14) 
                        { 
                            //photo gallery
                            main.progressBar1.Value = 0;
                            pro = 0;
                            //flag_gallery = true;
                            gallery();
                            
                        
                        }
                        


                    }
                }

            }

            else if (color_right.X < 1176 && color_right.X > 1130 && color_right.Y > 9 && color_right.Y < 100)
            {
               // if (main.Combo.IsDropDownOpen ==false)
                {
                    main.Combo.IsDropDownOpen = true;
                }
               // else main.Combo.IsDropDownOpen = false;
                

            }
            else if (color_right.X > 1180 && color_right.Y > 7 && color_right.Y < 120 && main.Combo.IsDropDownOpen == true)
            {

                pro = pro + 2;
                main.progressBar1.Value = pro;
               // SelectedIndex = "1";
                index = (color_right.X - (1138 +25)) / 50;
                main.Combo.SelectedIndex = index;
                if (pro == 100)
                {
                   //open gesture menu
                    if (main.Combo.IsDropDownOpen)
                    {
                       // main.label1.Content = index;
                        index = (color_right.X - (1138 + 25)) / 50;
                        if (index == 0)
                        {
                            gestType = "save";
                            flag_addGesture = true;

                        }
                        else if (index == 1)
                        {
                            gestType = "capture";

                            flag_addGesture = true;

                        }
                        else 
                        {

                            gestType = "close";
                            flag_addGesture = true;

                        }
                        //flag_addGesture = true;

                        main.Combo.IsDropDownOpen = false;
                        pro = 0;
                        main.progressBar1.Value = 0;
                    }
                }

            }


            else
            {
                pro = 0;
                main.progressBar1.Value = 0;

            }

            return false;
        }




        public void Draw(DepthImagePoint right, ColorImagePoint color_right, ColorImagePoint Elbow_right, DepthImagePoint Elbow)
        {

            // menuF = false;
            // main.menuitem.IsSubmenuOpen = false;
            if (!flag_checkGesture && !flag_addGesture && !flag_captureImage && !flag_gallery)
            {
                if (right.Depth < 2000)
                {
                    main.progressBar1.Value = 0;
                    pro = 0;

                    if (!flag_firstPoint)
                    {

                        Line line = new Line();


                        line.Stroke = brush.line2.Stroke;
                        line.StrokeThickness = brush.size;
                        line.X1 = p.X;
                        line.Y1 = p.Y;
                        line.X2 = color_right.X;
                        line.Y2 = color_right.Y;

                        // currentPoint = e.GetPosition(this);
                        p.X = color_right.X;
                        p.Y = color_right.Y;
                        main.canvas1.Children.Add(line);
                    }
                    else
                    {
                        main.icon_colors.Fill = new SolidColorBrush(Colors.Green);
                        p.X = color_right.X;
                        p.Y = color_right.Y;
                        flag_firstPoint = false;

                    }


                }
                else
                {
                    flag_firstPoint = true;

                    main.icon_colors.Fill = new SolidColorBrush(Colors.Red);
                }




            }
            if (flag_checkGesture)
            {
                string c = "";

                if (flag_addMenu)
                {
                    if (Convert.ToInt32(pro) == Convert.ToInt32((pro / 25)) * 25)
                    {

                        if (pro != 100)
                        {
                            points[Convert.ToInt32(pro) / 25, 0] = color_right.X - Elbow_right.X;
                            points[Convert.ToInt32(pro) / 25, 1] = color_right.Y - Elbow_right.Y;
                        }
                    }

                    main.ellipse8.Fill = checkGestureOn;
                    c = checkGesture();

                    if (c.Contains("save"))
                    {
                        command.save();
                        main.label1.Content = "save successfully";
                    }
                    else if (c.Contains("close"))
                    {
                        command.close();
                    }
                    else if (c.Contains("capture"))
                    {

                        flag_captureImage = true;

                    }
                    main.label1.Content = c;
                }


                else if (pro >= 100)
                {
                    flag_addMenu = true;
                    main.progressBar1.Value = 0;
                    pro = 0;

                }
                else { pro = pro + 3; main.progressBar1.Value = pro; }
                


            }

            else if (flag_addGesture)
            {

                if (flag_addMenu)
                {
                    if (Convert.ToInt32(pro) == Convert.ToInt32((pro / 25)) * 25)
                    {

                        if (pro != 100)
                        {
                            points[Convert.ToInt32(pro) / 25, 0] = color_right.X - Elbow_right.X;
                            points[Convert.ToInt32(pro) / 25, 1] = color_right.Y - Elbow_right.Y;
                        }
                    }
                    main.label1.Content = addGestures();
                }


                else if (pro >= 100)
                {
                    flag_addMenu = true;
                    main.progressBar1.Value = 0;
                    pro = 0;

                }
                else { pro = pro + 3; main.progressBar1.Value = pro; }
               
            }
            else if (flag_captureImage)
            {
                if (!command.Capture()) { main.ellipse14.Fill = captureImageOff; }
                else { main.ellipse14.Fill = captureImageOn; flag_captureImage = false; }

              //  main.label2.Content = flag5;

            }
            else if (flag_gallery) { 
            
                    if (right.Y < Elbow.Y)
                    {
                        swipe.runWorker();
                        swipe.setLRhand(1);
                        swipe.setRight_Hand(right.X);
                        swipe.setRight_Shoulder(main.rightShoulder.X);
                        swipe.setHead(main.head.X);
                         swipe.setLRhand(1);
                        main.textBlock4.Text = swipe.getRes();
                    }
                    
                if (main.left.Y < main.leftElbow.Y) {
                    swipe.runWorker();
                    swipe.setLRhand(2);
                    swipe.setLeft(main.left.X);
                    swipe.setleft_Shoulder(main.leftShoulder.X);
                    swipe.setHead(main.head.X);
                    swipe.setLRhand(2);
                    main.textBlock4.Text = swipe.getRes();
                }
            
            }
        }



        //display image on canvas
        public void displayImage(BitmapImage image) {

            main.galleryImage.Source = image;
        }
    }
}